<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Auth\events\Registered;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'roles' => $data['roles'],
            'password' => bcrypt($data['password']),
        ]);
    }

    // public function register(Request $request)
    // {
    //     // Here the request is validated. The validator method is located
    //     // inside the RegisterController, and makes sure the name, email
    //     // password and password_confirmation fields are required.
    //     $this->validator($request->all())->validate();
    //     // A Registered event is created and will trigger any relevant
    //     // observers, such as sending a confirmation email or any
    //     // code that needs to be run as soon as the user is created.
    //     event(new Registered($user = $this->create($request->all())));
    //     // After the user is created, he's logged in.
    //     $this->guard()->login($user);
    //     // And finally this is the hook that we want. If there is no
    //     // registered() method or it returns null, redirect him to
    //     // some other URL. In our case, we just need to implement
    //     // that method to return the correct response.
    //     return $this->registered($request, $user) ?: redirect($this->redirectPath());
    // }
    protected function registered(Request $request, $user)
    {
        $user->generateToken();
        return response()->json(['data' => $user->toArray()], 201);
    }
    // public function register(Request $request)
    //   {
    //         $user = new User;
    //         $user->email = $request->email;
    //         $user->name = $request->name;
    //         $user->roles = $request->roles;
    //         $user->api_token = str_random(60);
    //         $user->password = bcrypt($request->password);
    //         $user->save();
    //         return response([
    //             'status' => 'success',
    //             'data' => $user
    //            ], 200);
    //   }

}
